<?php
namespace addons\human\library;

use GuzzleHttp\Client;
use think\Cache;
use addons\human\model\Job;
use app\common\library\Auth;
use GuzzleHttp\Middleware;

class Speech{
    
    protected $baseUri;
    
    protected $config;
    
    
    public function __construct()
    {
        $config=get_addon_config('human');
        $this->baseUri=$config['endpoint'];
        $this->config=['key'=>$config['key']];
        
    }
    
    public function httpGet(string $url, array $query = [])
    {
        return $this->request($url, 'GET', ['query' => $query]);
    }

   
    public function httpPost(string $url, array $data = [])
    {
        return $this->request($url, 'POST', ['json' => $data]);
    }
    public function httpPut(string $url, array $data = [])
    {
        
        return $this->request($url, 'PUT', ['json' => $data]);
    }
    public function request(string $url, string $method = 'GET', array $options = [])
    {
        $headers= [
        'Ocp-Apim-Subscription-Key'=>$this->config['key'],
        'Content-Type' => 'application/json'
        ];
        $query=['api-version'=>"2024-04-15-preview"];
        if(array_key_exists('query',$options)){
           $options['query']['api-version']="2024-04-15-preview";
        }else{
           $options['query']= $query;
        }
        $client = new Client(['base_uri' => $this->baseUri,'headers'=>$headers]);
        try {
            $response = json_decode($client->request($method,$url, $options)->getBody()->getContents(),true);
            return $response;
            
        } catch (Exception $e ) {
            dump($e);
        }
        
    }
    
    
   
    // 提交合成信息
    // https://learn.microsoft.com/en-us/azure/ai-services/speech-service/language-support?tabs=tts
    public function submitSynthesis($content,$character='Lisa',$style='casual-sitting',$voice='zh-CN-XiaoxiaoNeural',$bg='#FFFFFFFF'){
        $job_id=time().rand(1000,9999);
        
        $data=[
            "synthesisConfig"=>["voice"=>$voice],
            "inputKind"=>"plainText",
            "inputs"=>[["content"=> $content]],
            "avatarConfig"=>[
                "customized"=>false,
                "talkingAvatarCharacter"=> $character,
                "talkingAvatarStyle"=>$style,
                "videoFormat"=> "mp4",
                "videoCodec"=>"h264",
                "subtitleType"=> "soft_embedded",
                "backgroundColor"=>$bg
                ]
            ];
        $user_id=Auth::instance()->id;
        Job::create(['job_id'=>$job_id,'user_id'=>$user_id]);     
        return $this->httpPut("/avatar/batchsyntheses/{$job_id}",$data);
    }
    // 获取任务状态
    public function getSynthesis($job_id){
        return $this->httpGet("/avatar/batchsyntheses/{$job_id}");
         
     }
     // 获取任务列表
    public function listSynthesisJobs($skip=0,$pageSize=10){
        $query=[
            'skip'=>$skip,
            'maxpagesize'=>$pageSize
            ];
        return $this->httpGet("/avatar/batchsyntheses",$query);
         
    }
    
    
    

}